package com.huiyeji.hui.controller;

import com.huiyeji.hui.auth.Authenticator;
import com.huiyeji.hui.auth.License;
import com.huiyeji.hui.entity.User;
import com.huiyeji.hui.service.ApplicationService;
import com.huiyeji.hui.utils.HuiResponse;
import com.huiyeji.hui.vo.GroupOrPrivateMessageVo;
import com.huiyeji.hui.vo.SystemMessageVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author zhaoduo
 */
@RestController
@RequestMapping("/message")
public class MessageController {

    private final Authenticator authenticator = Authenticator.getInstance();

    private final ApplicationService service;

    @Autowired
    public MessageController(ApplicationService service){
        this.service = service;
    }

    @GetMapping("/getSystemMessages")
    public HuiResponse<?> getSystemMessages(HttpSession session){
        authenticator.check(session, License.GENERAL);
        List<SystemMessageVo> vos = service.getSystemMessages();
        return HuiResponse.ok(vos);
    }

    @GetMapping("/getGroupMessages/{gid}")
    public HuiResponse<?> getGroupMessages(HttpSession session,
                                           @PathVariable("gid") Long gid){
        User user = authenticator.check(session, License.GENERAL);
        List<GroupOrPrivateMessageVo> vos = service.getGroupMessages(user.getUid(),gid);
        return HuiResponse.ok(vos);
    }

    @GetMapping("/getPrivateMessages/{fid}")
    public HuiResponse<?> getPrivateMessages(HttpSession session,
                                           @PathVariable("fid") Long fid){
        User user = authenticator.check(session, License.GENERAL);
        List<GroupOrPrivateMessageVo> vos = service.getPrivateMessages(user.getUid(),fid);
        return HuiResponse.ok(vos);
    }
}
